package com.bauer.base.designpattern.chainofresponsibility.sevletfilterimpl;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author ：rui.wei
 * @Date ：Created in 16:22 2019/7/8
 * @Description：
 */
public class FilterChain implements Filter {


    private Queue<Filter> filters = new LinkedList<>();

    public FilterChain add(Filter filter) {
        filters.add(filter);
        return this;
    }


    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {

        while (!filters.isEmpty()) {
            Filter filter = filters.poll();

            if (!filter.doFilter(request, response, filterChain)) {
                return false;
            }
        }
        return true;
    }
}
